using Random, Distributions, CairoMakie
Random.seed!(1234)
# the probability of success
ps = [0.1, 0.3, 0.5, 0.9]
# the number of experiments
ns = [10, 100, 1000]
layouts = [[1, 1], [1, 2], [2, 1], [2, 2]]
colors = [:blue, :green, :red]
normalDist = Normal(0, 1)
N = 10^6
fig = Figure(size=(1400, 1200))
for i in 1:length(ps)
p = ps[i]
ax = Axis(fig[layouts[i][1], layouts[i][2]])
totalBins = []
for j in 1:length(ns)
n = ns[j]
binomialDist = Binomial(n, p)
samples = rand(binomialDist, N)
# normal standardization
normSamples = @. (samples - n * p) / sqrt(n * p * (1 - p))
bins = sort(unique(normSamples))
stephist!(ax, normSamples; color=colors[j], normalization=:pdf, bins=bins, label=string("n = ", n))
totalBins = vcat(totalBins, bins)
end
xGrid = round(minimum(totalBins), RoundDown; digits=0):0.01:round(maximum(totalBins), RoundUp; digits=0)
lines!(ax, xGrid, pdf.(normalDist, xGrid); color=:black, label="N(0, 1)")
axislegend(ax)
ax.xlabel = string("x\n(p = ", p, ")")
ax.ylabel = "Density"
end
fig